<?php
namespace Buu\Modules\HaohaoceManager\Lib;
use Symfony\Component\Yaml\Yaml;
use Overtrue\Pinyin\Pinyin;
use Doraemon\tools\Log;

class Util{
    const CONFIG_DIR = '/data/wwwroot/g_buu/Modules/HaohaoceManager/Config';

	public static function read_conf(){
		$dp = opendir(self::CONFIG_DIR);
        while ($line = trim(readdir($dp))) {
            $filepath = self::CONFIG_DIR.'/'.$line;
            $pathinfo = pathinfo($filepath);
            if($pathinfo['extension'] == 'yaml'){
            	list($pod_type , $tmp) = explode('.' , $pathinfo['basename']);
                $podTypes[$pod_type] = Yaml::parse(file_get_contents($filepath));
            }
        }
        return $podTypes;
	}

	public static function name_to_wx_at($name){
		$whitelist = array(
			'xinshaopu' => 'xinshaopu@haohaozhu.com',
			'辛少普' => 'xinshaopu@haohaozhu.com',
			'田慧宁' => 'tianhuining@haohaozhu.com',
			'tianhuining' => 'tianhuining@haohaozhu.com',
			'巫峡' => 'wuxia@haohaozhu.com', 
			'wuxia' => 'wuxia@haohaozhu.com', 
		);
		if(isset($whitelist[$name])){
			return $whitelist[$name];
		}
		$pinyin = new Pinyin(); 
		$aPinyin = $pinyin->convert($name);
		$wx_account = '';
		foreach ($aPinyin as $key => $value) {
			$wx_account.=ucfirst($value);
		}
		return $wx_account;
	}

	public static function wxRobotMsg($url , $msg , $atList = [] , $is_markdown = false){
		
		$msgtype = $is_markdown ? 'markdown':'text';
		$data = array(
			'msgtype' => $msgtype,
			$msgtype => array(
				'content' => $msg,
				'mentioned_list' => $atList
			)
		);
		$data = json_encode($data);
        $result = self::curl_post($url, $data);
        return $result;
	}

	public static function add2domainList($pod_id , $domain_suffix){
		$url = 'https://buu.haohaozhu.me/api/domain/add';
		$domain_name = '容器测试环境-'.$domain_suffix.'-'.$pod_id;
		$host_json_tpl='{ "strack": "http://track.qa{suffix}.haohaoce.com/Sensorsdata/track"
		, "slog": "http://track.qa{suffix}.haohaoce.com/Sensorsdata/log"
		, "fapi": "http://fapi.qa{suffix}.haohaoce.com/"
		, "upload": "https://supload.haohaozhu.cn/"
		, "mh5": "http://m.qa{suffix}.haohaoce.com/"
		, "th5": "http://t.qa{suffix}.haohaoce.com/"
		, "capi": "http://api.qa{suffix}.haohaoce.com/"
		, "yapi": "http://yapi.qa{suffix}.haohaoce.com/"
		, "api": "http://api.qa{suffix}.haohaoce.com/"
		, "tapi": "http://tapi.qa{suffix}.haohaoce.com/" 
		, "imapi": "http://im-api.haohaoce.com/" 
		, "uploadAvatar": "http://callback-upload.haohaozhu.net/" 
		, "im": "im-api.haohaoce.com"
		, "im_port": "3100"
		, "imapi_port": "80"}';
		$host_json = str_replace('{suffix}' , $domain_suffix , $host_json_tpl);

		$post = ['domain_name' => $domain_name , 'host_json' => $host_json];
	
		$rs = json_decode(self::curl_post($url , $post) , 1);
		return $rs['code'] == '0' ? true : false;
	}
	public static function delFromDomainList($pod_id , $domain_suffix){
		$url = 'https://buu.haohaozhu.me/api/domain/del';
		$domain_name = '容器测试环境-'.$domain_suffix.'-'.$pod_id;
		$post = ['domain_name' => $domain_name];
		$rs = json_decode(self::curl_post($url , $post) , 1);
		if($rs['code'] == '0'){
			return true;
		}else{
			Log::debug($domain_name .' '. json_encode($rs));	
			return false;
		}
		
		
	}
	private static function curl_post($url , $post){
		
		$ch = curl_init();//初始化curl
        curl_setopt($ch, CURLOPT_URL,$url);//抓取指定网页
        curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
        curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
        $data = curl_exec($ch);//运行curl
        curl_close($ch);
        return $data;
	}
}